package neurones;

public class Neurone {
	
	private Reseau r;
	private int level;
	private boolean isCalc;
	private double value;
	private double[] links; //links[i] --> poid de la liaison vers le neurone i de la couche level-1
	private double delta = 0; // pour apprentissage;
	
	public Neurone(int nb_link, int level, Reseau r)
	{
		this.r = r;
		this.level = level;
		links = new double[nb_link];
		value = 0;
		setCalc(false);
	}
	
	public double getValue()
	{
		double result;
		
		if (!isCalc())
		{
			result = 0;
			for(int i = 0; i < links.length; i++) result += r.getN(level - 1, i).getValue() * links[i];
			
			result = 1 / (1 + Math.exp(-result));
			setCalc(true);
			value = result;
		}
		
		return value;
	}
	
	public void randomize()
	{
		for(int i = 0; i < links.length; i++) links[i] = (Math.random() - 0.5)*10;
		setCalc(false);
	}
	
	public void setValue(double value)
	{
		this.value = value;
		setCalc(true);
	}

	/**
	 * @param delta the delta to set
	 */
	public void setDelta(double delta) {
		this.delta = delta;
	}

	/**
	 * @return the delta
	 */
	public double getDelta() {
		return delta;
	}

	/**
	 * @param isCalc the isCalc to set
	 */
	public void setCalc(boolean isCalc) {
		this.isCalc = isCalc;
	}

	/**
	 * @return the isCalc
	 */
	public boolean isCalc() {
		return isCalc;
	}

	/**
	 * @param links the links to set
	 */
	public void setLinks(int neurone, double links) {
		this.links[neurone] = links;
	}

	/**
	 * @return the links
	 */
	public double getLinks(int neurone) {
		return links[neurone];
	}

}
